<template>
  <a-modal
    :title="`新增会员`"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="
      () => {
        $emit('ok')
      }
    "
    @cancel="
      () => {
        $emit('cancel')
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-form-item label="姓名">
          <a-input v-decorator="['name']" placeholder="请输入姓名" name="name" />
        </a-form-item>
        <a-form-item label="电话">
          <a-input
            v-decorator="[
              'phone',
              {
                rules: [
                  { required: true, message: '请输入电话' },
                  { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' },
                ],
              },
            ]"
            placeholder="请输入电话"
            name="phone"
          />
        </a-form-item>
        <a-form-item label="会员等级">
          <a-select v-decorator="['level']" placeholder="请选择" name="level">
            <a-select-option value="0">普卡</a-select-option>
            <a-select-option value="1">VIP</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="卡号">
          <a-input v-decorator="['cardNo']" placeholder="请输入卡号" name="cardNo" />
        </a-form-item>
        <a-form-item label="加入日期">
          <a-date-picker v-decorator="['joinDate']" style="width: 100%" valueFormat="YYYY-MM-DD"/>
        </a-form-item>
        <a-form-item label="身份证号">
          <a-input
            v-decorator="[
              'idCardNo',
              {
                rules: [
                  { required: true, message: '请输入身份证' },
                  {
                    pattern: /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
                    message: '请输入正确的身份证',
                    trigger: 'blur',
                  },
                ],
              },
            ]"
            placeholder="请输入身份证号"
            name="idCardNo"
          />
        </a-form-item>
        <a-form-item label="性别">
          <a-select v-decorator="['sex']" placeholder="请选择" name="sex">
            <a-select-option value="2">未知</a-select-option>
            <a-select-option value="1">男</a-select-option>
            <a-select-option value="0">女</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="出生日期">
          <a-date-picker v-decorator="['birthday']" style="width: 100%" valueFormat="YYYY-MM-DD"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>
          
          <script>
import pick from 'lodash.pick'
// 表单字段
const fields = ['name', 'phone', 'level', 'cardNo', 'joinDate', 'idCardNo', 'sex', 'birthday', 'id']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    loading: {
      type: Boolean,
      default: () => false,
    },
    model: {
      type: Object,
      default: () => null,
    },
  },
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 },
      },
    }
    return {
      sp: true,
      yys: true,
      AA: true,
      form: this.$form.createForm(this),
    }
  },
  created() {
    // 防止表单未注册
    fields.forEach((v) => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
}
</script>